package tech.kpretty.other;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
            System.out.println("召唤神龙");
        });

        for (int i = 0; i < 7; i++) {
            new Thread(() -> {
                System.out.println("集齐了一颗龙珠");
                try {
                    // 先到的线程阻塞在这里，等所有的线程都到才能继续往下走
                    cyclicBarrier.await();
                    System.out.println("神龙出现，冲呀！");
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
